var setting = {
	data : {
		key : {
			name : "orgName"
		},
		simpleData : {
			enable : true,
			idKey : "id",
			pIdKey : "parentId"
		}
	},
	check : {
		chkboxType : {"Y": "", "N": ""},//设置勾选既不影响父节点也不影响子节点
		enable : true
	},
	async : {
		enable : true,
		//otherParam: {"roleId":roleId},
		url : "admin/org/dept/getAllDeptList"
	},
	callback : {
		onClick : nodeClick,
		onCheck:nodeCheck
	}
};

var myTree;
var arrDept = new Array(); //项目所属组织集合,用来保存上传集合
$(document).ready(function() {
	myTree = $.fn.zTree.init($("#deptTree"), setting);
	
	//管理隶属增加 获得焦点显示组织列表
	$("#deptSelectView").focus(function () {
		$("#deptTree").show(1000);
    });
	$("input:not(#deptSelectView)").focus(function () {
    	$("#deptTree").hide(1000);
    });
	
});
//节点点击事件,用来控制点击节点名称勾选复选框
function nodeClick(event, treeId, treeNode) {
	//console.log(treeNode);
	if (treeNode.checked) { //当前节点为选中状态
		//取消选中
		myTree.checkNode(treeNode, false, true);
		arrDept.remove(treeNode);
	} else {
		myTree.checkNode(treeNode, true, true);
		arrDept.push(treeNode);
	}
	setDeptCorr();
}
//节点复选框事件,用来控制添加节点数据到数组
function nodeCheck(event, treeId, treeNode){
	if (treeNode.checked) { //当前节点为选中状态
		//取消选中
		arrDept.push(treeNode);
	} else {
		arrDept.remove(treeNode);
	}
	setDeptCorr();
}
//将arrDept数组中的值放到对应input(view,value)中
function setDeptCorr(){
	var view= $("#deptSelectView");
	var value=$("#deptSelectValue");
	var s="";
	var s1="";
	$.each(arrDept,function(i,v){
		s+=v.orgName;
		s1+=v.id;
		if(i!=arrDept.length-1){
			s+=",";
			s1+=",";
		}
	});
	view.val(s);
	value.val(s1);
}


//为数组创建删除指定元素方法
Array.prototype.indexOf = function(val) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == val) return i;
	}
	return -1;
};
Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
	if (index > -1) {
		this.splice(index, 1);
	}
};